package com.graph;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution547 {
    public int findCircleNum(int[][] isConnected) {
        int res = 0;
        boolean[] visited = new boolean[isConnected.length];
        for (int i = 0; i < isConnected.length; i++) {
            if (!visited[i]) {
                dfs(visited, isConnected, i);
                res++;
            }
        }
        return res;
    }

    public void dfs(boolean[] visited, int[][] isConnected, int i) {
        visited[i] = true;
        for (int j = 0; j < isConnected[i].length; j++) {
            if (!visited[j] && isConnected[i][j] == 1) dfs(visited, isConnected, j);
        }
    }
}
